﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Tailoring
{
    public partial class frmorder : Form
    {
        Boolean a = false;
        int clothrate = 0;
        SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
        public frmorder()
        {
            InitializeComponent();
        }
        string orderid;

        public string Orderid
        {
            get { return orderid; }
            set { orderid = value; }
        }
        public string pid { set { txtid.Text = value; } get { return txtid.Text; } }
        public string pname { set { txtname.Text = value; } get { return txtname.Text; } }
        public string pmobile { set { txtmobile.Text = value; } get { return txtmobile.Text; } }
        public string pcnic { set { txtcnic.Text = value; } get { return txtcnic.Text; } }
        public string cdate { set { txtcdate.Text = value; } get { return txtcdate.Text; } }
        public string ddate { set { txtddate.Text = value; } get { return txtddate.Text; } }
        public string otherdetail { set { txtother.Text = value; } get { return txtother.Text; } }
        public string returndays { set { cmbreturndays.Text = value; } get { return cmbreturndays.Text; } }
        public string nocloths { set { cmbnocloths.Text = value; } get { return cmbnocloths.Text; } }
        //public string otherdetail { set { txtother.Text = value; } get { return txtother.Text; } }
        //public string returndays { set { cmbreturndays.Text = value; } get { return cmbreturndays.Text; } }
        //public string nocloths { set { cmbnocloths.Text = value; } get { return cmbnocloths.Text; } }
        
        
        
        
        void saveorder()
        {
            try
            {


                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
                conn.Open();
                SqlCommand cmd = new SqlCommand("insertorder", conn);
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@cid", txtid.Text);

                cmd.Parameters.AddWithValue("@currentdate", txtcdate.Text);
                cmd.Parameters.AddWithValue("@delevrydate", txtddate.Text);
                cmd.Parameters.AddWithValue("@retday", cmbreturndays.Text.ToString());
                cmd.Parameters.AddWithValue("@noofcloths", cmbnocloths.Text);
                cmd.Parameters.AddWithValue("@orstatus", cmbstatus.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@cntid", txtid.Text);
                cmd.Parameters.AddWithValue("@totalamount", txtttotle.Text);
                cmd.Parameters.AddWithValue("@amountrec", txtrece.Text);
                cmd.Parameters.AddWithValue("@balnce", txtbal.Text);
                cmd.Parameters.AddWithValue("@paidstatus", cmbpaymentt.SelectedItem.ToString());

                cmd.Parameters.AddWithValue("@2salai", ck2salai.Checked);
                cmd.Parameters.AddWithValue("@ssalai", ckssalai.Checked);
                cmd.Parameters.AddWithValue("@ampatti", ckampatti.Checked);
                cmd.Parameters.AddWithValue("@2dagga", ck2dagga.Checked);
                cmd.Parameters.AddWithValue("@5kaj", ck5kaj.Checked);
                cmd.Parameters.AddWithValue("@wbtn", ckwaskatbtn.Checked);
                cmd.Parameters.AddWithValue("@vgalla", ckvgalla.Checked);
                cmd.Parameters.AddWithValue("@collar", ckcollar.Checked);
                cmd.Parameters.AddWithValue("@sidejaib", cksjaib.Checked);
                cmd.Parameters.AddWithValue("@tranDate", txtcdate.Text);
                cmd.Parameters.AddWithValue("@ambend", ckambend.Checked);
                cmd.Parameters.AddWithValue("@halfbend", ckhbend.Checked);
                cmd.Parameters.AddWithValue("@kurta", ckkurta.Checked);
                cmd.Parameters.AddWithValue("@golkaf", ckgkaf.Checked);
                cmd.Parameters.AddWithValue("@sadakaf", ckskaf.Checked);
                cmd.Parameters.AddWithValue("@sadapatti", ckspatti.Checked);
                cmd.Parameters.AddWithValue("@samnayjaib", cksamjaib.Checked);
                cmd.Parameters.AddWithValue("@shalwarjaib", ckshjaib.Checked);
                cmd.Parameters.AddWithValue("@sadaasteen", cksasteen.Checked);
                cmd.Parameters.AddWithValue("@other", txtother.Text);
                //if (conn.State == ConnectionState.Open)
                //{
                //    conn.Close();
                //}
                //cmd.Connection.Open();
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.InsertCommand = cmd;
                adapter.InsertCommand.ExecuteNonQuery();
                cmd.Connection.Close();
                MessageBox.Show("Record Order For Customer " + " " + txtname.Text + " " + "has Been Save Successfully ", "Save Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
            finally
            {
                if (a)
                {
                    // MessageBox.Show("Record Order For Customer " + " " + txtname.Text + " " + "has Been Save Successfully ", "Save Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    frmorderprint orderp = new frmorderprint();
                    orderp.Cid = Convert.ToInt32(txtid.Text);
                    orderp.ShowDialog();
                }
                this.conn.Close();
                this.Close();

            }



        }
        private void Oder_Form_Load(object sender, EventArgs e)
        {
            txtcdate.Text = DateTime.Now.ToString("dd MMMM yyyy");
            string cdate = txtcdate.Text;
            cmbpaymentt.SelectedIndex = 0;
            SqlConnection conn = new SqlConnection();
            conn = programcodes.getConnect();
            //conn.Open();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            frmcus test = new frmcus();
            test.Show();
        }

        private void cmbreturndays_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DateTime dt = DateTime.Now;
            //dt = dt.AddDays(Convert.ToDouble(cmbreturndays.SelectedItem));

            //txtddate.Text = dt.ToString("dd MMMM yyyy");
        }

        private void cmbreturndays_Leave(object sender, EventArgs e)
        {

            if (cmbreturndays.Text.Length < 1)
            {
                errorProvider1.SetError(cmbreturndays, "Return Days field is Empty Give a Value");
                cmbreturndays.Focus();
            }
            else
            {
                errorProvider1.Clear();
            }

        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            int c = 0;
            try
            {
                int a = Convert.ToInt32(txtttotle.Text);
                int b = Convert.ToInt32(txtrece.Text);
                c = a - b;
                txtbal.Text = Convert.ToString(c);
            }
            catch
            {

            }
            if (c < 0)
            {
                txtrece.Focus();
                errorProvider1.SetError(txtrece, "Value Exceded from Total Amount");
            }
            else
            {
                errorProvider1.Clear();
            }

        }


        private void cmbreturndays_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DateTime dt = DateTime.Now;
                dt = dt.AddDays(Convert.ToDouble(cmbreturndays.Text));

                txtddate.Text = dt.ToString("dd MMMM yyyy");
            }
            catch { txtddate.Text = ""; }
        }

        private void txtttotle_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtrece.Text = "0";
            //txtbal.Text = txtttotle.Text;
            string a = txtttotle.Text;
            if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
                e.Handled = true;
            if (a.Length >= 7)
                errorProvider1.SetError(txtttotle, "Something Wrong with Data Entry Only Digits!");
            else
                errorProvider1.Clear();

        }


        private void txtserial_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
                e.Handled = true;
        }

        private void txtrece_KeyPress(object sender, KeyPressEventArgs e)
        {
            string a = txtrece.Text;
            if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
                e.Handled = true;
            if (a.Length >= 7)
                errorProvider1.SetError(txtrece, "Something Wrong with Data Entry Only Digits!");
            else
                errorProvider1.Clear();
        }

        private void cmbpaymentt_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbpaymentt.Text == "Paid")
            {
                txtrece.Enabled = true;

                txtrece.Text = txtttotle.Text;
                txtbal.Text = "0";
            }
            else
            {
                txtrece.Text = "0";
                txtrece.Enabled = false;
                txtbal.Text = txtttotle.Text;
            }
            }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void ordersavemenu_Click(object sender, EventArgs e)
        {
            saveorder();
        }

        private void txtid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 13)
            {
                SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
                try
                {
                    conn.Open();
                    string strquery = "select * from tblclientinfo where clientId=" + txtid.Text;
                    SqlCommand sqlcmd = new SqlCommand(strquery, conn);

                    //sqlcmd.Parameters.AddWithValue("@id", txtid.Text);
                    SqlDataReader sqldreader = sqlcmd.ExecuteReader();
                    if (sqldreader.HasRows)
                    {
                        while (sqldreader.Read())
                        {
                            txtid.Text = sqldreader["clientId"].ToString();
                            txtname.Text = sqldreader["name"].ToString();
                            txtmobile.Text = sqldreader["mobile"].ToString();
                            txtcnic.Text = sqldreader["cnic"].ToString();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Record for ID" + " " + txtid.Text + " " + "Not Found !");
                    }
                    sqldreader.Close();
                    //if (sqldreader["clientId"] != null)
                    //{
                    //    lbstatus.Text = "This ID Exist Try Another One";
                    //}


                    //sqlcmd.Parameters.Add(new SqlParameter("@id", System.Data.DbType.Int32,10,"clientId"));
                    // Fill the parameter with the value retrieved
                    // from the text field
                    //cmd.Parameters["@Find"].Value = txtFind.Text;

                    //// Execute the query
                    //rdr = cmd.ExecuteReader();

                    //// Fill the list box with the values retrieved
                    //lbFound.Items.Clear();
                    //while (rdr.Read())
                    //{
                    //    lbFound.Items.Add(rdr["FirstName"].ToString() +
                    //    " " + rdr["LastName"].ToString());
                    //}


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void cmbnocloths_Leave(object sender, EventArgs e)
        {
            if (cmbnocloths.Text.Length < 1)
            {
                errorProvider1.SetError(cmbnocloths, "No of Cloths Field is Empty Give a Value");
                cmbnocloths.Focus();
            }
            else
            {
                errorProvider1.Clear();
            }
        }

        private void txtttotle_Leave(object sender, EventArgs e)
        {
            
            if (txtttotle.Text.Length < 1)
            {
                errorProvider1.SetError(txtttotle, "Field is Empty Give a Amount");
                //errorProvider1.Icon = SystemIcons.Information;
                txtttotle.Focus();
            }
            else
            {
                errorProvider1.Clear();
                errorProvider1.Icon = SystemIcons.Information;
            }
        }

        private void txtrece_MouseClick(object sender, MouseEventArgs e)
        {

            txtrece.Text = "";

        }

        private void txtrece_Leave(object sender, EventArgs e)
        {
            if (txtrece.Text == "")
            {
                txtrece.Text = "0";
            }
        }

        private void txtid_Click(object sender, EventArgs e)
        {
            txtid.Text = "";
        }

        private void ordersavnprint_Click(object sender, EventArgs e)
        {
            a = true;
            saveorder();


        }

        private void cmbnocloths_TextChanged(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
            try
            {
                conn.Open();
                string strquery = "select ClothRate from tblsetting";
                SqlCommand sqlcmd = new SqlCommand(strquery, conn);
                //sqlcmd.Parameters.AddWithValue("@id", txtid.Text);
                SqlDataReader sqldreader = sqlcmd.ExecuteReader();
                if (sqldreader.HasRows)
                {
                    while(sqldreader.Read())
                    clothrate = Convert.ToInt32(sqldreader["ClothRate"]);
                    txtttotle.Text = (clothrate * Convert.ToInt32(cmbnocloths.Text)).ToString();

                }
                else
                {
                    MessageBox.Show("Add Cloth Rate");
                    frmusercontrol sett = new frmusercontrol();
                    sett.tabcontrol.SelectedIndex=1;
                    sett.Show();
                    
                }
                sqldreader.Close();
                conn.Close();
            }
            catch
            {
                MessageBox.Show("Error: Number of Cloths Must be Given a Value" );

            }
            txtrece.Text = "0";
            txtbal.Text = txtttotle.Text;
            
        }

        private void cmbnocloths_Enter(object sender, EventArgs e)
        {

        }

        private void orderexitmenu_Click(object sender, EventArgs e)
        {
            if ((txtid.Text.Length > 0) || (txtname.Text.Length > 0) || (txtmobile.Text.Length > 0))
            {
                if (MessageBox.Show("Are you Sure to Close the Form?",
                          "Order Form Closing Confirmation",
                           MessageBoxButtons.YesNo,
                           MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    this.Close();
                }

            }
            else
            {
                this.Close();
            }
        }



    }
}

